package com.hc.oa.attendance.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.hibernate.HibernateUtils;
import com.hc.oa.attendance.dao.AttendanceDao;
import com.hc.oa.model.SlAttendance;


@Component("AttendanceService")
public class AttendanceService {
	 @Resource
	    private AttendanceDao AttendanceDao;
	     
	    @DataProvider
	    public Collection<SlAttendance> getAll(){
	        return AttendanceDao.getAll();
	    }
	     
	    @DataProvider
	    public void getAllForPage(Page<SlAttendance> page,Map<String, Object> parameter) throws Exception{
	    	ContextHolder ch = new ContextHolder();
			String UserName = ch.getLoginUserName();
			if(parameter==null)
			{
				 parameter = new HashMap<String, Object>();
				 parameter.put("employeeName", UserName);
			}
			else
			{
				parameter.put("employeeName",UserName);
			}
	        AttendanceDao.queryAllByPageWithMap(page,SlAttendance.class,parameter);
	    }
	     
	    @DataResolver
	    @Transactional
	    public void saveSlAttendance(Collection<SlAttendance> datas) {
	    	AttendanceDao.persistEntities(datas);
		}
	    
	    @DataProvider
	    public void getAllForFilter(Page<SlAttendance> page, Criteria criteria) throws Exception{
	        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(SlAttendance.class);
	        if (criteria != null){
	            AttendanceDao.find(page, HibernateUtils.createFilter(detachedCriteria, criteria));
	        }else{
	        	AttendanceDao.getAll(page);
	        }
	    }
	    
		@DataProvider
		public void getEmployeeByUserName(Page<SlAttendance> page) {
			ContextHolder ch = new ContextHolder();
			String UserName = ch.getLoginUserName();
			Map param = new HashMap();
			if (StringHelper.isNotEmpty(UserName)) {
				param.put("userName", UserName);
				 AttendanceDao.queryObjectall(page,SlAttendance.class, "employeeName",UserName);
			}
		}	    
		
	    @DataProvider
	    public void queryForCondition(Page<SlAttendance> page, Map<String, Object> params) {
	        if (null != params) {
	        	String ATTENDANCE_NUMBER = (String)params.get("ATTENDANCE_NUMBER");
	            String EMPLOYEE_ID = (String)params.get("EMPLOYEE_ID");
	            String EMPLOYEE_NAME = (String) params.get("EMPLOYEE_NAME");
	            String ATTENDANCE_YEAR = (String)params.get("ATTENDANCE_YEAR");
	            String ATTENDANCE_MONTH = (String)params.get("ATTENDANCE_MONTH");
	            String ATTENDANCE_DATE = (String)params.get("ATTENDANCE_DATE");
	            String ARRIVAL_TIME = (String)params.get("ARRIVAL_TIME");
	            String DEPARTURE_TIME = (String) params.get("DEPARTURE_TIME");
	            String WORK_TIME = (String)params.get("WORK_TIME");
	            String ATTENDANCE_TYPE = (String)params.get("ATTENDANCE_TYPE");
	            String COMMENTS = (String)params.get("COMMENTS");
	            
	            String whereCase = "";
	            if (StringHelper.isNotEmpty(ATTENDANCE_NUMBER)) {
	                whereCase += " AND ATTENDANCE_NUMBER '%" + ATTENDANCE_NUMBER + "%' ";
	            }
	            
	            if (StringHelper.isNotEmpty(EMPLOYEE_ID)) {
	                whereCase += " AND EMPLOYEE_ID '%" + EMPLOYEE_ID + "%' ";
	            }
	             
	            if (StringHelper.isNotEmpty(EMPLOYEE_NAME)) {
	                whereCase += " AND EMPLOYEE_NAME '%" + EMPLOYEE_NAME + "%' ";
	            }
	             
	            if (StringHelper.isNotEmpty(ATTENDANCE_YEAR)) {
	                whereCase += " AND ATTENDANCE_YEAR '%" + ATTENDANCE_YEAR + "%' ";
	            }
	             
	            if (StringHelper.isNotEmpty(ATTENDANCE_MONTH)) {
	                whereCase += " ATTENDANCE_MONTH '%" + ATTENDANCE_MONTH + "%' ";
	            }
	            
	            if (StringHelper.isNotEmpty(ATTENDANCE_DATE)) {
	                whereCase += " ATTENDANCE_DATE '%" + ATTENDANCE_DATE + "%' ";
	            }
	             
	            if (StringHelper.isNotEmpty(ARRIVAL_TIME)) {
	                whereCase += " ARRIVAL_TIME '%" + ARRIVAL_TIME + "%' ";
	            }
	            
	            if (StringHelper.isNotEmpty(DEPARTURE_TIME)) {
	                whereCase += " DEPARTURE_TIME '%" + DEPARTURE_TIME + "%' ";
	            }
	            
	            if (StringHelper.isNotEmpty(WORK_TIME)) {
	                whereCase += " WORK_TIME '%" + WORK_TIME + "%' ";
	            }
	           
	            if (StringHelper.isNotEmpty(ATTENDANCE_TYPE)) {
	                whereCase += " ATTENDANCE_TYPE '%" + ATTENDANCE_TYPE + "%' ";
	            }
	            
	            if (StringHelper.isNotEmpty(COMMENTS)) {
	                whereCase += " COMMENTS '%" + COMMENTS + "%' ";
	            }
	            
	            AttendanceDao.find(page, " from SlAttendance where 1=1 " + whereCase);
	        } else
	            AttendanceDao.getAll(page);
	    }
}
